home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Imaging / RealShapes / RealShpe.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  7.2 KB  |  382 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        RealShpe.h
  3.  
  4.     Contains:    RealShape class, private to ODShape.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.         <10>     8/29/95    jpa        Don't use kODErrInvalidValue. [1278284]
  13.          <9>     5/25/95    jpa        Use new GX headers [1241078, 1253324]
  14.          <8>     2/24/95    jpa        Removed private ODNewRgn. [1220810]
  15.          <7>     12/5/94    jpa        Code review cleanup [1203923]
  16.          <6>    10/24/94    jpa        Added NewGeometricShape [1195070]. Added
  17.                                     ODNewRgn: allocate rgns from temp-mem.
  18.                                     [1151710]
  19.          <5>     9/29/94    RA        1189812: Mods for 68K build.
  20.          <4>      8/2/94    jpa        Added AsPolygonShape method.
  21.          <3>     7/26/94    jpa        Initialize gGX to 99, not -1 [it's
  22.                                     unsigned]. Actually any value >1 will do.
  23.          <2>     6/18/94    MB        Update memory includes
  24.          <1>     6/15/94    jpa        first checked in
  25.                             --------Moved to SOM project and rearranged-----
  26.          <9>     5/10/94    jpa        Replaced 'inherited::' to appease %$#@
  27.                                     cfront.
  28.          <8>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  29.          <7>      5/9/94    jpa        Enable polygon clipper and GX support
  30.                                     [1162090]
  31.          <6>     3/25/94    JA        Use new XMPTransform API. (1153438)
  32.          <5>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  33.                                     #1150864.
  34.          <4>     2/17/94    JA        Include new AltPoly.h.
  35.          <3>     2/16/94    JA        Stop using pointers to member fns. Include
  36.                                     new AltPoint.h.
  37.          <2>      2/9/94    JA        ShapePrv.h --> ShapePvM.h
  38.         <10>      2/7/94    JA        Capitalized THROW.
  39.          <9>      2/7/94    JA        Further tigerings.
  40.          <8>      2/3/94    JA        Tiger Team Makeover!
  41.          <7>     1/31/94    JA        Mucho fixes to polygon shapes & region
  42.                                     shapes. Other bugs squashed. Etc.
  43.          <6>     12/3/93    TÇ        Stop including XMPError.h, it is included
  44.                                     as ErrorDef.h inside Except.h
  45.          <5>    11/29/93    JA        Fixed XMPRectShape::Union.
  46.          <4>    11/24/93    JA        cfront fixes.
  47.          <3>    11/24/93    VL        Rolled back changes as ASLM build breaks
  48.                                     the THINK build.
  49.          <2>    11/23/93    VL        Made this work with ASLM.
  50.          <1>    11/23/93    JA        first checked in
  51.  
  52.     In Progress:
  53.         
  54. */
  55.  
  56.  
  57. #ifndef _ALTPOINT_
  58. #include "AltPoint.h"            /* Use C++ savvy XMPPoint and XMPRect*/
  59. #endif
  60.  
  61. #ifndef _ALTPOLY_
  62. #include "AltPoly.h"
  63. #endif
  64.  
  65.  
  66. #ifndef _REALSHPE_
  67. #include "RealShpe.h"
  68. #endif
  69.  
  70. #ifndef _GXSHPE_
  71. #include "GXShpe.h"
  72. #endif
  73.  
  74. #ifndef _POLYSHPE_
  75. #include "PolyShpe.h"
  76. #endif
  77.  
  78. #ifndef _RECTSHPE_
  79. #include "RectShpe.h"
  80. #endif
  81.  
  82. #ifndef _RGNSHPE_
  83. #include "RgnShpe.h"
  84. #endif
  85.  
  86. #ifndef _SHAPE_
  87. #include "Shape.xh"
  88. #endif
  89.  
  90. #ifndef _EXCEPT_
  91. #include "Except.h"
  92. #endif
  93.  
  94. #ifndef _ODMEMORY_
  95. #include "ODMemory.h"
  96. #endif
  97.  
  98. #ifndef __GXGRAPHICS__
  99. #include <GXGraphics.h>
  100. #endif
  101.  
  102. #ifndef _ODDEBUG_
  103. #include "ODDebug.h"
  104. #endif
  105.  
  106. #ifndef _UTILERRS_
  107. #include <UtilErrs.h>
  108. #endif
  109.  
  110.  
  111. //==============================================================================
  112. // QuickDraw GX Utilities
  113. //==============================================================================
  114.  
  115.  
  116. ODBoolean gGX = 99;        // Is GX installed? Initialized by ODShape constructor
  117.  
  118.  
  119. #pragma segment QDGXShape
  120.  
  121.  
  122. void
  123. ClearGXError( )
  124. {
  125.     GXGetGraphicsError(kODNULL);
  126.     // GX error status is cleared after asking for errors.
  127. }
  128.  
  129.  
  130. void
  131. ThrowIfGXError( )
  132. {
  133.     gxGraphicsError err = GXGetGraphicsError(kODNULL);    // Get latest graphics error
  134.     if( err )
  135.         THROW(err,"QuickDraw GX error");
  136. }
  137.  
  138.  
  139. void
  140. ThrowIfFirstGXError( )
  141. {
  142.     gxGraphicsError err;
  143.     (void) GXGetGraphicsError(&err);            // Get first error, not last
  144.     if( err )
  145.         THROW(err,"QuickDraw GX error");
  146. }
  147.  
  148.  
  149. //==============================================================================
  150. // RealShape
  151. //==============================================================================
  152.  
  153.  
  154. #pragma segment ODShape
  155.  
  156.  
  157. RealShape::RealShape( ODGeometryMode mode )
  158.     :fQDRegion(kODNULL),
  159.      fMode(mode)
  160. {
  161. }
  162.  
  163.  
  164. RealShape::~RealShape( )
  165. {
  166.     this->Purge(0);
  167. }
  168.  
  169.  
  170. ODSize
  171. RealShape::Purge( ODSize )
  172. {
  173.     if( fQDRegion ) {
  174.         ODSize size = GetHandleSize((Handle)fQDRegion);
  175.         DisposeRgn(fQDRegion);
  176.         fQDRegion = kODNULL;
  177.         return size;
  178.     } else
  179.         return 0;
  180. }
  181.  
  182.  
  183. RealShape*
  184. RealShape::NewGeometricShape( ODGeometryMode mode ) //static method
  185. {
  186.     if( gGX )
  187.         return new QDGXShape(mode);
  188.     else
  189.         return new PolygonShape(mode);
  190. }
  191.  
  192.  
  193. void
  194. RealShape::SetGeometryMode( ODGeometryMode mode )
  195. {
  196.     if( mode==kODNeedsGeometry && !this->HasGeometry() )
  197.         THROW(kODErrNoShapeGeometry);
  198.         
  199.     fMode = mode;
  200. }
  201.  
  202.  
  203. ODGeometryMode
  204. RealShape::GetGeometryMode( )
  205. {
  206.     return fMode;
  207. }
  208.  
  209.  
  210. RealShape*
  211. RealShape::SetPolygon( const ODPolygon &poly )
  212. {
  213.     ODRect r;
  214.     if( poly.AsRectangle(&r) )
  215.         return this->SetRectangle(&r);
  216.         
  217.     else {
  218.         RealShape *s;
  219.         s = RealShape::NewGeometricShape(fMode);
  220.         s = s->SetPolygon(poly);
  221.         delete this;
  222.         return s;
  223.     }
  224. }
  225.  
  226.  
  227. void
  228. RealShape::Simplify( )
  229. {
  230.     // Only PolygonShape does anything in this method.
  231. }
  232.  
  233.  
  234. ODPlatformShape
  235. RealShape::GetPlatformShape( ODGraphicsSystem system )
  236. {
  237.     if( system==kODQuickDraw ) {
  238.         if( !fQDRegion ) {
  239.             fQDRegion = ODNewRgn();
  240.             TRY{
  241.                 this->InitQDRegion();
  242.             }CATCH_ALL{
  243.                 DisposeRgn(fQDRegion);
  244.                 fQDRegion = kODNULL;
  245.                 RERAISE;
  246.             }ENDTRY
  247.         }
  248.         return fQDRegion;
  249.         
  250.     } else if( system==kODQuickDrawGX && gGX )
  251.         return this->CopyGXShape();
  252.         
  253.     else {
  254.         THROW(kODErrInvalidGraphicsSystem);
  255.         return kODNULL;
  256.     }
  257. }
  258.  
  259.  
  260. void
  261. RealShape::SetPlatformShape( ODGraphicsSystem, ODPlatformShape )
  262. {
  263.     WARN("RealShape::SetPlatformShape should have been overridden by subclass!");
  264.     THROW(kODErrInvalidGraphicsSystem);
  265. }
  266.  
  267.  
  268. ODBoolean
  269. RealShape::HasGeometry( )
  270. {
  271.     return kODTrue;    // ODRgnShape overrides this to return false if non-rectangular
  272. }
  273.  
  274.  
  275. RealShape*
  276. RealShape::Clear( )
  277. {
  278.     // Delete me and return a new empty rect-shape:
  279.     
  280.     ODRect empty(0,0,0,0);
  281.     RealShape *s = new RectShape(fMode,empty);
  282.     delete this;
  283.     return s;
  284. }
  285.  
  286.  
  287. RealShape*
  288. RealShape::ReplaceWith( RealShape *shape )
  289. {
  290.     RealShape *s = shape->Copy();
  291.     delete this;
  292.     return s;
  293. }
  294.  
  295.  
  296. RealShape*
  297. RealShape::Combine( ODShapeOp op, RealShape *shape )
  298. {
  299.     switch( op ) {
  300.         case kShapeIntersection:
  301.             return this->Intersect(shape);
  302.         case kShapeUnion:
  303.             return this->Union(shape);
  304.         case kShapeDifference:
  305.             return this->Subtract(shape);
  306.         case kShapeNoOp:
  307.             return this;
  308. #if ODDebug
  309.         default:
  310.             WARN("Bogus ShapeOp to Combine()");
  311.             THROW(kODErrAssertionFailed);
  312.             break;
  313. #endif
  314.     }
  315.     return kODNULL;
  316. }
  317.  
  318.  
  319. RealShape*
  320. RealShape::AsPolygonShape( )
  321. {
  322.     RealShape *s = kODNULL; ODVolatile(s);
  323.     ODPolygon poly;            ODVolatile(poly);
  324.     
  325.     this->CopyPolygon(poly);
  326.     TRY{
  327.         s = RealShape::NewGeometricShape(fMode);
  328.         s->SetPolygon(poly);
  329.     }CATCH_ALL{
  330.         delete s;
  331.         poly.Clear();
  332.         RERAISE;
  333.     }ENDTRY
  334.     poly.Clear();
  335.     
  336.     return s;
  337. }
  338.  
  339.  
  340. RealShape*
  341. RealShape::Promote( ODShapeOp op, RealShape *shape )
  342. {
  343.     // Delete me and return an equivalent region- or polygon-shape.
  344.     // Optionally perform a binary operation on the promoted shape.
  345.     
  346.     RealShape *s; ODVolatile(s);
  347.     
  348.     if( fMode!=kODLoseGeometry && (shape==kODNULL || shape->HasGeometry()) ) {
  349.         s = this->AsPolygonShape();
  350.         TRY{
  351.             s = s->Combine(op,shape);
  352.         }CATCH_ALL{
  353.             delete s;
  354.             RERAISE;
  355.         }ENDTRY
  356.  
  357.     } else {
  358.         if( fMode==kODNeedsGeometry )
  359.             THROW(kODErrNoShapeGeometry);
  360.  
  361.         RgnHandle rgn = (RgnHandle) ODCopyHandle(
  362.                                         (ODHandle)this->GetPlatformShape(kODQuickDraw) );
  363.         TRY{
  364.             s = new RgnShape(fMode);
  365.         }CATCH_ALL{
  366.             DisposeRgn(rgn);
  367.             RERAISE;
  368.         }ENDTRY
  369.         
  370.         TRY{
  371.             s->SetPlatformShape(kODQuickDraw,rgn);
  372.             s = s->Combine(op,shape);
  373.         }CATCH_ALL{
  374.             delete s;
  375.             RERAISE;
  376.         }ENDTRY
  377.     }
  378.  
  379.     delete this;
  380.     return s;
  381. }
  382.